package demo;

/**
 *   删除链表中倒数第n个节点
 *
 */
class Solution38 {
	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode dummy = head;
		ListNode fast = head;
		ListNode pre = null;
		while (fast != null) {
			fast = fast.next;
			if (n == 0) {
				pre = head;
				head = head.next;
			} else {
				n--;
			}
		}
		if (head != null && pre != null) {
			pre.next = head.next;
		} else if (pre == null && head != null) {
			return head.next;
		} else {
			return head;
		}
		return dummy;
	}
}

